﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.Globalization;
using System.Threading;
using ZO.SmartCore.Protocols.XMPP.Core;
using ZO.SmartCore.Xml;

#endregion

namespace ZO.SmartCore.Protocols.XMPP.Protocol.Common
{
    /// <summary>
    /// base class for all "primary" JABBER elements within the XML stream..
    /// </summary>
    public abstract class Packet : Element
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="Packet"/> class.
        /// </summary>
        /// <param name="name">Name of the element.</param>
        protected Packet(string name) : base(name) { } // Packet


        /// <summary>
        /// Initializes a new instance of the <see cref="Packet"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="ns">The ns.</param>
        protected Packet(string name, string value, string ns) : base(name, value, ns) { } // Packet

        /// <summary>
        /// Initializes a new instance of the <see cref="Packet"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="ns">The ns.</param>
        /// <param name="prefix">The prefix.</param>
        protected Packet(string name, string value, string ns, string prefix) : base(name, value, ns, prefix) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Packet"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <param name="ns">The ns.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="ns2">The NS2.</param>
        protected Packet(string name, string value, string ns, string prefix, string ns2) : base(name, value, ns, prefix, ns2) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Packet"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        protected Packet(string name, string value)
            : base(name, value)
        {
        } // Packet

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private static long id;
        private static string _UniquedIDPrefix = "XmppLib-1-0-";

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets The 'from' attribute.
        /// </summary>
        /// <value>specifies the JID of the sender.</value>
        public JID From
        {
            get
            {
                if (this.Attributes.Contains("from"))
                {
                    return new JID(this.GetAttribute("from"));
                }
                return null;
            }
            set
            {
                if (value != null)
                {
                    this.SetAttribute("from", value.ToString());
                }
                else
                {
                    this.Attributes.Remove("from");
                }
            }
        }


        /// <summary>
        /// Gets or sets the ID.
        /// </summary>
        /// <value>unique identifier created by the receiving entity.</value>
        public string ID
        {
            get
            {
                return this.GetAttribute("id");
            }
            set
            {
                this.SetAttribute("id", value);
            }
        }

        /// <summary>
        /// Gets or sets 'to' attribute.
        /// </summary>
        /// <value>specifies the JID of the intended recipient.</value>
        public JID To
        {
            get
            {
                if (this.Attributes.Contains("to"))
                {
                    return new JID(this.GetAttribute("to"));
                }
                return null;
            }
            set
            {
                if (value != null)
                {
                    this.SetAttribute("to", value.ToString());

                }
                else
                {
                    this.Attributes.Remove("to");
                }
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets the next ID.
        /// </summary>
        /// <returns></returns>
        internal static string GetNextID()
        {
            Interlocked.Increment(ref id);
            return _UniquedIDPrefix + id.ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Reset the id counter to agsXmpp_1 again
        /// </summary>
        internal static void ResetID()
        {
            id = 0;
        }

        /// <summary>
        /// to Save Bandwidth on Mobile devices you can change the prefix
        /// null is also possible to optimize Bandwidth usage
        /// </summary>
        internal static string UniqueIDPrefix
        {
            get { return _UniquedIDPrefix; }
            set
            {
                if (String.IsNullOrEmpty(value))
                    _UniquedIDPrefix = "";
                else
                    _UniquedIDPrefix = value;
            }
        }

        /// <summary>
        /// Switches the from and to attributes 
        /// </summary>
        public void SwitchDirection()
        {
            JID from = this.From;
            JID to = this.To;

            // Remove from and to now
            this.RemoveAttribute("from");
            this.RemoveAttribute("to");

            //swap attributes
            this.From = to;
            this.To = from;
        } // SwitchDirection

        /// <summary>
        /// Generates a automatic id for the packet.
        /// </summary>
        internal void GenerateID()
        {
            this.ID = GetNextID();
        }   

        #endregion
    }
}
